﻿var app = angular.module('app', []);

app.controller('bodyController', function ($scope) {
    var $video = $("#main-video");
     $video.css('min-height', $video.width() / 16 * 9 + 'px');

    var player = videojs('main-video', {
        controls: true,
        //sources: [{
        //    src: '/video/v1.mp4',
        //    type: 'video/mp4'
        //}]
        //  language:'zh-CN'
    });

    window.player = player;

    $("body").removeClass("pre-body");

    $scope.videoList = [
        { name: '第1集', link: 'ql8/1', enabled: true },
        { name: '第2集', link: 'ql8/2', enabled: true },
        { name: '第3集', link: 'ql8/3', enabled: true },
        { name: '第4集', link: 'ql8/4', enabled: true },
        { name: '第5集', link: 'ql8/5', enabled: false },
        { name: '第6集', link: 'ql8/6', enabled: false }
    ];

    $scope.changeVideo = function (item) {
        if (!item.enabled) { 
            return;
        }
        location.hash = item.link;
        refreshHash();
    };

    function refreshHash() {
        if (!location.hash) {
            location.hash = $scope.videoList[0].link;
        }


        var url = location.hash.replace('#', '');

        var match = $.grep($scope.videoList, function (item) { 
            return item.link === url;
        })[0];
        if (!match) {
            url = location.hash = $scope.videoList[0].link;
        }

        $scope.curHash = location.hash;

        var link = '/video/' + url + '.mp4';
        player.src({ src: link });
    }
    refreshHash();

    heatCheck();
    function heatCheck() {
       // $.post('./');
        var videoTime = player.currentTime();
        var videoLink = location.hash.replace('#', '');

        var postData = {
            videoTime: videoTime,
            videoLink: videoLink
        };
        $.post('./Home/HeartCheck', postData);

        setTimeout(function () {
            heatCheck();
        }, 1000 * 60);
    }
});

